//app.js
const QQMapWX = require('./resources/lib/qqmap-wx-jssdk.min')
App({
    onLaunch: function() {
        this.init()
        this.wxLocation()
        this.acctoken()
        if (wx.canIUse('getUpdateManager')) {
            const updateManager = wx.getUpdateManager()
            updateManager.onCheckForUpdate(function (res) {
              // 请求完新版本信息的回调
              if (res.hasUpdate) {
                // console.log('res.hasUpdate====')
                updateManager.onUpdateReady(function () {
                  wx.showModal({
                    title: '更新提示',
                    content: '新版本已经准备好，是否重启应用？',
                    success: function (res) {
                      // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
                      if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate()
                      }
                    }
                  })
                })
                updateManager.onUpdateFailed(function () {
                  // 新的版本下载失败
                  wx.showModal({
                    title: '已经有新版本了哟~',
                    content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
                  })
                })
              }
            })
          }
    },
    init: function() {
        let userInfo = this.getUserInfo()
        this.globalData.userInfo = userInfo
        // if (!userInfo) {
        //     wx.reLaunch({
        //         url: '/pages/index/index'
        //     })
        // } else {

        // }
    },
    openSetting() {
        return new Promise((resolve, reject) => {
            wx.openSetting({
                success: res => {
                    res.authSetting = {
                        'scope.userLocation': true
                    }
                    if (
                        res.errMsg == 'openSetting:ok' &&
                        res.authSetting['scope.userLocation']
                    ) {
                        resolve()
                    } else {
                        reject()
                    }
                }
            })
        })
    },
    wxLocation: function() {
        const qqmapsdk = new QQMapWX({
            key: 'FLRBZ-7QUKQ-7XF54-GBG6J-PHIU7-ZPBYJ'
        })
        let _this = this
        wx.getLocation({
            success: res => {
                wx.showLoading({
                    title: '位置获取中...'
                })
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: res.latitude,
                        longitude: res.longitude
                    },
                    success: function(res) {
                        setTimeout(() => {
                            wx.hideLoading()
                        }, 500)
                        if (res.status == 0) {
                            _this.globalData.currentAddress = res.result;
                            if (_this.LocaInfoReadyCallback) {
                              _this.LocaInfoReadyCallback(res.result);
                            }
                            wx.setStorage({
                              key: 'currentAddress',
                              data: res.result,
                            })
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'none',
                                duration: 1000
                            })
                        }
                    }
                })
            },
            fail: res => {
                  wx.showModal({
                    title: '警告',
                    content:
                        '亲，您拒绝了微信授权，将无法正常使用小程序，请重新授权',
                    confirmText: '重新授权',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            _this
                            .openSetting()
                            .then(() => {
                                _this.wxLocation()
                            })
                            .catch(() => {
                            })
                         
                        }
                    }
                })
                wx.getSystemInfo({
                    success: function (res) {
                        if (res.system.indexOf('iOS') == 0) {
                            wx.navigateTo({
                                url: '/pages/setings/index'
                            })
                        } else {
                        //   _this.wxLocation()
                        }
                    }
                    })
            }
        })
    },
    getUserInfo: function() {
        return wx.getStorageSync('userInfo')
    },
    
    acctoken:function(){
        let access_token = wx.getStorageSync('access_token');
        if (access_token != null){
            //直接进行检测操作
        }
        let url = 'https://api.weixin.qq.com/cgi-bin/token';
        wx.request({
            url: url,
            method : 'GET',
            data : {
            grant_type: 'client_credential',
            appid: 'wxa49a840f735fbade',
            secret: '5260c1d863b5fe59f702be65392874df'
            },
            success : function(res){
               wx.setStorageSync('access_token', res.data.access_token)
            //正常返回结果
            //{"access_token":"ACCESS_TOKEN","expires_in":7200}
            }
        })
    },
    //实现全局监听globalData
    /*
    setWatching: function(key, method) {
        let obj = this.globalData
        //加个前缀生成隐藏变量，防止死循环发生
        let ori = obj[key] //obj[key]这个不能放在Object.defineProperty里
        
        if (ori) {
            //处理已经声明的变量，绑定处理
            method(ori)
        }
        Object.defineProperty(obj, key, {
            configurable: true,
            enumerable: true,
            set: function(value) {
                this['___' + key] = value
                method(value) //数据有变化的时候回调函数，实现同步功能
            },
            get: function() {
                if (typeof this['__' + key] == 'undefined') {
                    if (ori) {
                        //这里读取数据的时候隐藏变量和 globalData设置不一样，所以要做同步处理
                        this['__' + key] = ori
                        return ori
                    } else {
                        return undefined
                    }
                } else {
                    return this['__' + key]
                }
            }
        })
    },
    */
    globalData: {
        userInfo: null,
        currentAddress: null,
        selarea:{
          'district':'',
          'adcode':''
        },
        messageTypeList: [],
        merchantsTypeList: [],
        addtype: [],
        swiperList1: [],
        ediddata:""
    },
    set_sel_area: function (data){
      let _this = this;
      this.globalData.selarea=data;
    }
})
